/**
 * 0102
 *  
 * @Author : roryrywang
 * @DateTime : 2024/8/16 14:59
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include "queue"

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

// 递归 栈
//class Solution {
//public:
//    vector<int> tmp;
//    vector<vector<int>> res;
//
//    void dfs(TreeNode *node, int k) {
//        if (node == nullptr) return;
//        if (res.size() < k + 1) res.push_back({});
//        res[k].push_back(node->val);
//        dfs(node->left, k + 1);
//        dfs(node->right, k + 1);
//    }
//
//    vector<vector<int>> levelOrder(TreeNode *root) {
//        dfs(root, 0);
//        return res;
//    }
//};

// 队列
class Solution {
public:
    struct Node {
        TreeNode *node;
        int k;
    };

    vector<vector<int>> levelOrder(TreeNode *root) {
        vector<vector<int>> res;
        if (root == nullptr) return res;

        queue<Node> q;
        q.push({root, 0});
        while (!q.empty()) {
            Node node = q.front();
            q.pop();
            if (res.size() < node.k + 1)res.push_back({});
            res[node.k].push_back(node.node->val);
            if (node.node->left != nullptr)q.push({node.node->left, node.k + 1});
            if (node.node->right != nullptr)q.push({node.node->right, node.k + 1});
        }

        return res;
    }
};

int main() {

    return 0;
}